<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="consultationApplyList"
      @selection-change="handleSelectionChange"
      border
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" type="index" width="50" align="center">
      </el-table-column>
      <!-- <el-table-column
          label="会诊申请ID"
          align="center"
          prop="id"
          v-if="true"
        /> -->
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column
        label="会诊申请内容"
        align="center"
        prop="consultationContent"
      />
      <!-- <el-table-column label="接受人ID" align="center" prop="receiveManId" /> -->
      <el-table-column
        label="接收时间"
        align="center"
        prop="receiveTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" prop="isDispose" />
      <el-table-column label="处理内容" align="center" prop="disposeContent" />
      <el-table-column
        label="处理时间"
        align="center"
        prop="disposeTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.disposeTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['workstation:consultationApply:edit']"
              >处理</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['workstation:consultationApply:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会诊申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户姓名" prop="userName">
          <el-input
            v-model="form.userName"
            placeholder="请输入用户姓名"
            :disabled="disabled"
          />
          <!-- <el-autocomplete
              style="width: 100%;"
              v-model="form.userName"
              :fetch-suggestions="querySearchAsync"
              placeholder="请输入用户姓名"
              @select="handleSelect"
              :disabled="disabled"
            ></el-autocomplete> -->
        </el-form-item>
        <el-form-item label="会诊申请内容">
          <!-- <editor v-model="form.consultationContent" :min-height="192" /> -->
          <el-input
            :disabled="disabled"
            type="textarea"
            :rows="4"
            v-model="form.consultationContent"
            placeholder="请输入会诊申请内容"
          />
        </el-form-item>
        <!-- <el-form-item label="接受人ID" prop="receiveManId">
            <el-input v-model="form.receiveManId" placeholder="请输入接受人ID" />
          </el-form-item>
          <el-form-item label="接收时间" prop="receiveTime">
            <el-date-picker
              clearable
              v-model="form.receiveTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择接收时间"
            >
            </el-date-picker>
          </el-form-item> -->
        <el-form-item label="处理内容">
          <!-- <editor v-model="form.disposeContent" :min-height="192" /> -->
          <el-input
            type="textarea"
            :rows="4"
            v-model="form.disposeContent"
            placeholder="请输入处理内容"
          />
        </el-form-item>
        <!-- <el-form-item label="处理时间" prop="disposeTime">
            <el-date-picker
              clearable
              v-model="form.disposeTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择处理时间"
            >
            </el-date-picker>
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConsultationApply,
  getConsultationApply,
  delConsultationApply,
  addConsultationApply,
  updateConsultationApply,
} from "@/api/workstation/consultationApply/consultationApply.js";
import { listBaseInfo } from "@/api/workstation/baseInfo";

export default {
  name: "ConsultationApply",
  data() {
    return {
      disabled: false, // 会诊内容输入框是否禁用
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会诊申请表格数据
      consultationApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        consultationContent: undefined,
        receiveManId: undefined,
        receiveTime: undefined,
        disposeContent: undefined,
        disposeTime: undefined,
        medicalRecordNo: this.$route.query.medicalRecordNo + "",
      },
      medicalRecordNo: "", // 路由传过来的药历号
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "会诊申请ID不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
        ],
        consultationContent: [
          { required: true, message: "会诊申请内容不能为空", trigger: "blur" },
        ],
        receiveManId: [
          { required: true, message: "接受人ID不能为空", trigger: "blur" },
        ],
        receiveTime: [
          { required: true, message: "接收时间不能为空", trigger: "blur" },
        ],
        disposeContent: [
          { required: true, message: "处理内容不能为空", trigger: "blur" },
        ],
        disposeTime: [
          { required: true, message: "处理时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.medicalRecordNo = this.$route.query.medicalRecordNo + "";
    // this.getList();
  },
  methods: {
    /** 查询会诊申请列表 */
    getList() {
      this.loading = true;
      listConsultationApply(this.queryParams).then((response) => {
        this.consultationApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        consultationContent: undefined,
        receiveManId: undefined,
        receiveTime: undefined,
        disposeContent: undefined,
        disposeTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disabled = false;
      this.reset();
      this.open = true;
      this.title = "添加会诊申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disabled = true;
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getConsultationApply(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "处理会诊申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateConsultationApply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("处理成功");
                this.open = false;
                this.disabled = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addConsultationApply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除会诊申请编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delConsultationApply(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "workstation/consultationApply/export",
        {
          ...this.queryParams,
        },
        `consultationApply_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
